/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace OctopusMx {
	namespace Utils {
		public static void texture_cache_insert (string name, Gdk.Pixbuf pixbuf) {
			try {
				Clutter.Texture texture = pixbuf_to_texture (pixbuf);
				Mx.TextureCache.get_default ().insert (name, texture.get_cogl_texture ());
			} catch (GLib.Error e) {
				GLib.warning ("error inserting into texture cache: %s", e.message);
			}
		}

		private static Clutter.Texture pixbuf_to_texture (Gdk.Pixbuf pixbuf) {
			//this is the new code, we would like to use
			//but get_texture () not in vapi
			/*
			Clutter.Image texture = new Clutter.Image ();
			texture.set_data (pixbuf.get_pixels (),
			                  pixbuf.has_alpha ? Cogl.PixelFormat.RGBA_8888
			                                   : Cogl.PixelFormat.RGB_888,
			                  pixbuf.width,
			                  pixbuf.height,
			                  pixbuf.rowstride);
			Mx.TextureCache.get_default ().insert (name, texture.get_texture ());
			*/

			//public bool set_from_rgb_data ([CCode (array_length = false)] uint8[] data,
			//                               bool has_alpha,
			//                               int width, int height,
			//                               int rowstride,
			//                               int bpp,
			//                               Clutter.TextureFlags flags) throws GLib.Error;
			Clutter.Texture texture = new Clutter.Texture ();
			texture.set_from_rgb_data (pixbuf.get_pixels (),
			                           pixbuf.has_alpha,
			                           pixbuf.width,
			                           pixbuf.height,
			                           pixbuf.rowstride,
			                           pixbuf.has_alpha ? 4 : 3,
			                           Clutter.TextureFlags.NONE);
			return texture;
		}

		//constructor can't have parameters in js
		public static Mx.Application new_application (string application_id, GLib.ApplicationFlags flags) {
			return new Mx.Application (application_id, flags);
		}

		public static void window_set_icon (Mx.Window window, Gdk.Pixbuf pixbuf) {
			try {
				Clutter.Texture texture = pixbuf_to_texture (pixbuf);
				window.set_icon_from_cogl_texture (texture.get_cogl_texture ());
			} catch (GLib.Error e) {
				GLib.warning ("error set window icon: %s", e.message);
			}
		}
	}
}
